import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _some from "lodash/some";
import _map from "lodash/map";
import _isArray from "lodash/isArray";
import _forEach from "lodash/forEach";

var _objectSpread2, _objectSpread3, _objectSpread4, _objectSpread5, _ref, _objectSpread6, _objectSpread7, _objectSpread8, _objectSpread9, _ref2, _objectSpread10, _objectSpread11, _objectSpread12, _ref3, _ref4;

/**
 * utils
 * 个性化用到的方法和数据
 * @date 2018/9/27
 */
import React from 'react';
import { Select } from 'antd';
import DynamicFormComponent from 'components/DynamicComponent/DynamicForm';
import DynamicTableComponent from 'components/DynamicComponent/DynamicTable';
import intl from 'utils/intl';
import DataType from "./DataType";
import { fieldLabelProp, attributeTypeProp, attributeNameProp, attributeValueProp } from "./config"; // const commonComponentInitialValues = {};

var commonFieldInitialValues = {
  enabledFlag: 1,
  visiableFlag: 1,
  requiredFlag: 0
}; // 组件（表单）

var Input = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread2 = {
  name: '文本'
}, _defineProperty(_objectSpread2, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.input').d('文本')), _defineProperty(_objectSpread2, "componentType", 'Input'), _defineProperty(_objectSpread2, "className", 'inputClassName'), _defineProperty(_objectSpread2, "config", []), _objectSpread2));

var InputNumber = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread3 = {
  name: '数值'
}, _defineProperty(_objectSpread3, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.inputNumber').d('数值')), _defineProperty(_objectSpread3, "componentType", 'InputNumber'), _defineProperty(_objectSpread3, "className", 'numberClassName'), _defineProperty(_objectSpread3, "config", []), _objectSpread3));

var TextArea = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread4 = {
  name: '多行文本'
}, _defineProperty(_objectSpread4, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.textArea').d('多行文本')), _defineProperty(_objectSpread4, "componentType", 'TextArea'), _defineProperty(_objectSpread4, "className", 'inputClassName'), _defineProperty(_objectSpread4, "config", []), _objectSpread4));

var DatePicker = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread5 = {
  name: '日期'
}, _defineProperty(_objectSpread5, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.datePicker').d('日期')), _defineProperty(_objectSpread5, "componentType", 'DatePicker'), _defineProperty(_objectSpread5, "className", 'dateClassName'), _defineProperty(_objectSpread5, "config", []), _objectSpread5));

var ValueList = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread6 = {
  name: '下拉列表'
}, _defineProperty(_objectSpread6, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.valueList').d('下拉列表')), _defineProperty(_objectSpread6, "componentType", 'ValueList'), _defineProperty(_objectSpread6, "className", 'selectClassName'), _defineProperty(_objectSpread6, "config", [(_ref = {}, _defineProperty(_ref, attributeNameProp, 'lazyLoad'), _defineProperty(_ref, attributeValueProp, true), _defineProperty(_ref, attributeTypeProp, DataType.Boolean), _ref)]), _objectSpread6));

var Lov = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread7 = {
  name: 'Lov'
}, _defineProperty(_objectSpread7, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.lov').d('Lov')), _defineProperty(_objectSpread7, "componentType", 'Lov'), _defineProperty(_objectSpread7, "className", 'lovClassName'), _defineProperty(_objectSpread7, "config", []), _objectSpread7));

var Switch = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread8 = {
  name: '开关'
}, _defineProperty(_objectSpread8, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.switch').d('开关')), _defineProperty(_objectSpread8, "componentType", 'Switch'), _defineProperty(_objectSpread8, "className", 'switchClassName'), _defineProperty(_objectSpread8, "config", []), _objectSpread8));

var Checkbox = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread9 = {
  name: 'checkbox'
}, _defineProperty(_objectSpread9, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.checkbox').d('checkbox')), _defineProperty(_objectSpread9, "componentType", 'Checkbox'), _defineProperty(_objectSpread9, "className", 'checkboxClassName'), _defineProperty(_objectSpread9, "config", []), _objectSpread9));

var Button = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread10 = {
  name: '按钮'
}, _defineProperty(_objectSpread10, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.button').d('按钮')), _defineProperty(_objectSpread10, "componentType", 'Button'), _defineProperty(_objectSpread10, "className", 'buttonClassName'), _defineProperty(_objectSpread10, "config", [(_ref2 = {}, _defineProperty(_ref2, attributeNameProp, 'style.marginRight'), _defineProperty(_ref2, attributeTypeProp, DataType.Number), _defineProperty(_ref2, attributeValueProp, 20), _ref2)]), _objectSpread10)); // 组件（表格）


var LinkButton = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread11 = {
  name: '行按钮'
}, _defineProperty(_objectSpread11, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.linkButton').d('行按钮')), _defineProperty(_objectSpread11, "componentType", 'LinkButton'), _defineProperty(_objectSpread11, "className", 'buttonClassName'), _defineProperty(_objectSpread11, "config", []), _defineProperty(_objectSpread11, "btns", []), _objectSpread11));

var ColumnField = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, (_objectSpread12 = {
  name: '列字段'
}, _defineProperty(_objectSpread12, fieldLabelProp, intl.get('cpfm.ui.model.fieldType.columnField').d('列字段')), _defineProperty(_objectSpread12, "componentType", 'ColumnField'), _defineProperty(_objectSpread12, "className", 'buttonClassName'), _defineProperty(_objectSpread12, "config", []), _objectSpread12)); // 组件（Tabs）


export var TabPane = _objectSpread(_objectSpread({}, commonFieldInitialValues), {}, {
  name: '标签',
  fieldLabel: intl.get('cpfm.ui.model.fieldType.TabPane').d('标签'),
  componentType: 'TabPane',
  className: 'modalClassName',
  config: []
}); // 导出的所有 TPL

export var templates = {}; // 表格

var DynamicTable = {
  name: '表格',
  templateType: 'DynamicTable',
  className: 'tableClassName',
  config: [(_ref3 = {}, _defineProperty(_ref3, attributeTypeProp, DataType.Boolean), _defineProperty(_ref3, attributeNameProp, 'pagination'), _defineProperty(_ref3, attributeValueProp, true), _ref3), (_ref4 = {}, _defineProperty(_ref4, attributeTypeProp, DataType.Number), _defineProperty(_ref4, attributeNameProp, 'defaultPageSize'), _defineProperty(_ref4, attributeValueProp, 10), _ref4)],
  accepts: [LinkButton, ColumnField],
  enabledFlag: 1,
  description: 'cpfm.ui.component.dynamicTable',
  defaultIntlDescription: '表格'
}; // 表单

var DynamicForm = {
  templateType: 'DynamicForm',
  className: 'formClassName',
  config: [],
  accepts: {
    Input: Input,
    TextArea: TextArea,
    InputNumber: InputNumber,
    DatePicker: DatePicker,
    ValueList: ValueList,
    Lov: Lov,
    Switch: Switch,
    Checkbox: Checkbox
  },
  enabledFlag: 1,
  description: 'cpfm.ui.component.dynamicForm',
  defaultIntlDescription: '表单'
}; // 按钮组

var DynamicToolbar = {
  name: '按钮组',
  templateType: 'DynamicToolbar',
  className: 'toolbarClassName',
  config: [],
  accepts: {
    Button: Button
  },
  enabledFlag: 1,
  description: 'cpfm.ui.component.dynamicToolbar',
  defaultIntlDescription: '按钮组'
}; // 模态框

var DynamicModal = {
  name: '弹出框',
  templateType: 'DynamicModal',
  className: 'modalClassName',
  config: [],
  accepts: {// DynamicForm,
    // DynamicToolbar,
    // DynamicTable,
    // // eslint-disable-next-line
    // DynamicModal,
  },
  enabledFlag: 1,
  description: 'cpfm.ui.component.dynamicModal',
  defaultIntlDescription: '弹出框'
}; // Tabs

var DynamicTabs = {
  name: '标签页',
  templateType: 'DynamicTabs',
  className: 'modalClassName',
  config: [],
  fields: [],
  // 存储 tab 的信息
  children: [],
  // 储存 子组件的信息
  accepts: templates,
  enabledFlag: 1,
  description: 'cpfm.ui.component.dynamicTabs',
  defaultIntlDescription: '标签页'
}; // 所有的容器（component）, 左侧组件区域用到

templates.DynamicForm = DynamicForm;
templates.DynamicToolbar = DynamicToolbar;
templates.DynamicTable = DynamicTable;
templates.DynamicModal = DynamicModal;
templates.DynamicTabs = DynamicTabs; // 为 toolbar 的 按钮可以触发的事件

export function getTemplateTriggerOptions(templatesConfig) {
  var optionGroups = [];

  _forEach(templatesConfig, function (template) {
    var optGroupOptions = null;

    switch (template.templateType) {
      case 'DynamicForm':
        optGroupOptions = [/*#__PURE__*/React.createElement(Select.Option, {
          key: "reset",
          value: "this.ref['".concat(template.templateCode, "'].reset")
        }, "reset")];
        break;

      case 'DynamicTable':
        optGroupOptions = [/*#__PURE__*/React.createElement(Select.Option, {
          key: "query",
          value: "this.ref['".concat(template.templateCode, "'].query")
        }, "query"), /*#__PURE__*/React.createElement(Select.Option, {
          key: "reload",
          value: "this.ref['".concat(template.templateCode, "'].reload")
        }, "reLoad")];
        break;

      default:
        break;
    }

    if (optGroupOptions !== null) {
      optionGroups.push( /*#__PURE__*/React.createElement(Select.OptGroup, {
        label: template.description,
        key: template.templateCode
      }, optGroupOptions));
    }
  });

  return optionGroups;
}
export function getQueryFormOptions(templatesConfig) {
  var formOptions = [];

  _forEach(templatesConfig, function (template) {
    switch (template.templateType) {
      case 'DynamicForm':
        // todo
        formOptions.push( /*#__PURE__*/React.createElement(Select.Option, {
          key: template.templateCode,
          value: "this.ref['".concat(template.templateCode, "'].props.form")
        }, template.description));
        break;

      default:
        break;
    }
  });

  return formOptions;
}
/**
 * todo 每次新增组件都需要修改
 * 获取 tpl 对应的 export 的方法
 */

export function getTemplateFuncs(templatesConfig) {
  var templateFuncOptions = [];

  _forEach(templatesConfig, function (template) {
    var templateComponent;

    switch (template.templateType) {
      case 'DynamicForm':
        templateComponent = DynamicFormComponent;
        break;

      case 'DynamicTable':
        templateComponent = DynamicTableComponent;
        break;

      default:
        break;
    } // todo


    if (templateComponent) {
      templateFuncOptions.push( /*#__PURE__*/React.createElement(Select.OptGroup, {
        key: template.templateCode,
        label: template.description
      }, _map(templateComponent.triggers, function (func) {
        return /*#__PURE__*/React.createElement(Select.Option, {
          key: func,
          value: "this.ref['".concat(template.templateCode, "'].").concat(func)
        }, templateComponent.triggerMeaning[func]);
      })));
    }
  });

  return templateFuncOptions;
}
/**
 * todo 每次新增tpl可能都需要更改
 * 用于比较之后更新和template相关的事情
 * 顺序 和 templateCode相等才认为是相等的
 * @param {Object[]} prevTemplates
 * @param {Object[]} nextTemplates
 * @returns {boolean}
 */

export function templatesIsNoEqual(prevTemplates, nextTemplates) {
  if (_isArray(prevTemplates) && _isArray(nextTemplates)) {
    if (!(prevTemplates.length === 0 && nextTemplates.length === 0)) {
      if (prevTemplates.length !== nextTemplates.length) {
        return true;
      } else {
        var isEq = true;

        for (var i = 0; i < prevTemplates.length; i++) {
          isEq = prevTemplates[i].templateCode === nextTemplates[i].templateCode;

          if (!isEq) {
            break;
          }
        }

        return !isEq;
      }
    }
  }

  return true;
}
/**
 * todo 每次新增 tpl 都需要判断这里需不需要更改
 * 能接收 tpl 作为 field 的组件 是否包含 child tpl
 * @param {object} parent - 能接收 tpl 的 tpl
 * @param {object} child - tpl
 * @returns {boolean} parent 包含 child
 */

export function hasTemplate(parent, child) {
  switch (parent.templateType) {
    case 'DynamicTabs':
      return _some(parent.fields, function (field) {
        return _some(field.children, function (tpl) {
          return hasTemplate(tpl, child);
        });
      });

    default:
      return parent === child;
  }
}